// pages/order/order.js
var app = getApp();
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: ["全部订单", "待付款", "待配送", "待确认", "已完成"],
    currentTab: 0,
    order_list: [
      [],
      [],
      [],
      [],
      []
    ],
    is_show: false,
    flag: false,
    click_flag: false, //防止重复点击
    cur_page: 1,
    state_type: "",
    hasOrder: true //是否有相关订单
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options);
    var that = this;

    this.setData({
      currentTab: options.currentTab,
    })
    var current = options.currentTab;
    if (current == 0) {
      that.setData({
        state_type: "",
      })
    }
    if (current == 1) {
      that.setData({
        state_type: "state_new",
      })
    }
    if (current == 2) {
      that.setData({
        state_type: "state_nosend",
      })
    }
    if (current == 3) {
      that.setData({
        state_type: "state_send",
      })
    }
    if (current == 4) {
      that.setData({
        state_type: "state_success",
      })
    }
    this.initData();
  },

  initData: function() {
    var that = this;
    that.setData({
      cur_page: 1,
    })
    util.postUrl("member_order/order_list", {
      cur_page: that.data.cur_page,
      state_type: that.data.state_type,
    }, function(res) {
      var order_list = that.data.order_list;
      var temp = res.data.datas.order_list;

      order_list[that.data.currentTab] = temp;
      that.setData({
        order_list: order_list
      })
      console.log(that.data.order_list);
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    if (app.quit_id != "") {
      this.initData();
      app.quit_id = "";
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  /** 
   * 滑动切换tab 
   */
  bindChange: function(e) {
    var that = this;
    var current = e.detail.current;
    that.setData({
      currentTab: e.detail.current
    });
    if (current == 0) {
      that.setData({
        state_type: "",
      })
    }
    if (current == 1) {
      that.setData({
        state_type: "state_new",
      })
    }
    if (current == 2) {
      that.setData({
        state_type: "state_nosend",
      })
    }
    if (current == 3) {
      that.setData({
        state_type: "state_send",
      })
    }
    if (current == 4) {
      that.setData({
        state_type: "state_success",
      })
    }
    that.initData();
  },

  /** 
   * 点击tab切换 
   */
  swichNav: function(e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      var scrollLeftNumber = 0;
      var current = e.currentTarget.dataset.current;
      if (current > 4) {
        scrollLeftNumber = 4 * 130;
        scrollLeftNumber = scrollLeftNumber + 130 * (current - 4);
      }
      that.setData({
        currentTab: e.target.dataset.current,
        scrollLeftNumber: scrollLeftNumber
      })
    }
  },
  //跳转订单详情
  goOrderDetail: function(e) {
    var item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '../order_detail/order_detail?order_id=' + item.order_id,
    })
  },

  //上拉加载
  handlescrolltolower: function() {
    var that = this;
    util.postUrl("member_order/order_list", {
      cur_page: that.data.cur_page + 1,
      state_type: that.data.state_type,
    }, function(res) {
      var res_list = res.data.datas.order_list
      var l = Object.keys(res_list).length;
      console.log(l, res_list);
      if (l > 0) {
        var order_list = that.data.order_list;
        var temp_list = order_list[that.data.currentTab];
        for (var key in res_list) {
          temp_list[key] = res_list[key];
        }
        order_list[that.data.currentTab] = temp_list;
        that.setData({
          order_list: order_list,
          cur_page: that.data.cur_page + 1,
        })
        console.log(that.data.cur_page, that.data.order_list);
      } else {
        wx.showToast({
          title: '无更多数据',
          icon: 'none'
        })
      }

    })
    console.log("拉倒底部")
  },
  //支付
  payReq: function(e) {
    var that = this;
    console.log(e);
    var pay_sn = e.currentTarget.dataset.index;
    var item = e.currentTarget.dataset.item;
    util.postUrl("buy/prepay_id", {
      pay_sn: pay_sn,
      open_id: wx.getStorageSync("open_id")
    }, function(pre_res) {
      if (pre_res.data.code == 200) {
        console.log("buy/prepay_id", pre_res);
        var reciver_info = pre_res.data.datas.reciver_info;
        reciver_info.area_info = reciver_info.area;
        reciver_info.address = reciver_info.street;
        reciver_info.true_name = pre_res.data.datas.reciver_name;
        wx.requestPayment({
          'timeStamp': pre_res.data.datas.pay_info.timeStamp + "",
          'nonceStr': pre_res.data.datas.pay_info.nonceStr,
          'package': pre_res.data.datas.pay_info.package,
          'signType': 'MD5',
          'paySign': pre_res.data.datas.pay_info.paySign,
          'success': function(res) {
            console.log("支付=", res);
            wx.redirectTo({
              url: '../buy_finish/buy_finish?sum=' + item.price + "&address_info=" + JSON.stringify(reciver_info),
            })
          },
          'fail': function(res) {}
        })
      } else {
        wx.showToast({
          title: res.data.datas.error,
          icon: 'none',
        })
      }
    })
  },
  cancelOrder: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var order_index = e.currentTarget.dataset.order_index;
    var real_list = that.data.order_list;
    var order_list = that.data.order_list[that.data.currentTab];
    var item = order_list[order_index].list[index];
    console.log(item);
    var pay_sn = item.pay_sn;
    wx.showModal({
      title: '提示',
      content: '确认要取消该订单?',
      success: function(res) {
        if (res.confirm) {
          util.postUrl("member_order/order_cancel", {
            order_id: item.order_id,
          }, function(res) {
            if (res.data.code == 200) {
              util.postUrl("member_order/order_list", {
                pay_sn: pay_sn,
              }, function(res) {
                var temp_list = res.data.datas.order_list;
                console.log(real_list[that.data.currentTab][order_index], temp_list[order_index]);
                real_list[that.data.currentTab][order_index] = temp_list[order_index];
                that.setData({
                  order_list: real_list,
                })
                wx.showToast({
                  title: '取消成功',
                  icon: 'none'
                })
              })

            }
          })
        }
      }
    })
  },
  delOrder: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var order_index = e.currentTarget.dataset.order_index;
    var real_list = that.data.order_list;
    var order_list = that.data.order_list[that.data.currentTab];
    var item = order_list[order_index].list[index];
    console.log(item);
    var pay_sn = item.pay_sn;
    wx.showModal({
      title: '提示',
      content: '确认要删除该订单?',
      success: function(res) {
        if (res.confirm) {
          util.postUrl("member_order/order_delete", {
            order_id: item.order_id,
          }, function(res) {
            if (res.data.code == 200) {
              util.postUrl("member_order/order_list", {
                pay_sn: pay_sn,
              }, function(res) {
                var temp_list = res.data.datas.order_list;
                console.log(real_list[that.data.currentTab][order_index], temp_list[order_index]);
                real_list[that.data.currentTab][order_index] = temp_list[order_index];
                that.setData({
                  order_list: real_list,
                })
                wx.showToast({
                  title: '删除成功',
                  icon: 'none'
                })
              })

            }
          })
        }
      }
    })
  },
  orderRec: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var order_index = e.currentTarget.dataset.order_index;
    var real_list = that.data.order_list;
    var order_list = that.data.order_list[that.data.currentTab];
    var item = order_list[order_index].list[index];
    console.log(item);
    var pay_sn = item.pay_sn;
    wx.showModal({
      title: '提示',
      content: '确认收货该订单?',
      success: function(res) {
        if (res.confirm) {
          util.postUrl("member_order/order_receive", {
            order_id: item.order_id,
          }, function(res) {
            if (res.data.code == 200) {
              util.postUrl("member_order/order_list", {
                pay_sn: pay_sn,
              }, function(res) {
                var temp_list = res.data.datas.order_list;
                console.log(real_list[that.data.currentTab][order_index], temp_list[order_index]);
                real_list[that.data.currentTab][order_index] = temp_list[order_index];
                that.setData({
                  order_list: real_list,
                })
                wx.showToast({
                  title: '确认收货成功',
                  icon: 'none'
                })
              })
            }
          })
        }
      }
    })


  },
  quitOrder: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var order_index = e.currentTarget.dataset.order_index;
    var real_list = that.data.order_list;
    var order_list = that.data.order_list[that.data.currentTab];
    var item = order_list[order_index].list[index];
    console.log(item);
    wx.navigateTo({
      url: '../apply_quit/apply_quit?order=' + JSON.stringify(item),
    })
  },
  goShopDetail: function(e) {
    var item = e.currentTarget.dataset.item;
    console.log(item);

    wx.navigateTo({
      url: '../shop_detail/shop_detail?store_id=' + item.store_id,
    })

  },
  goClass: function() {
    console.log("111");
    wx.switchTab({
      url: '../classify/classify',
    })
  }
})